package com.hoshiicloud.common.controller;

import lombok.extern.slf4j.Slf4j;
import org.springframework.validation.BindingResult;
import org.springframework.validation.FieldError;
import org.springframework.validation.ObjectError;

import java.util.HashMap;
import java.util.Map;


/**
 * controller基类
 *
 * @author : maiboyc
 * @date : 2019/4/29 09:43
 * @mail : maiboyc@163.com
 **/
@Slf4j
public class BaseController {

    /**
     * 处理验证信息
     * @param result 验证结果绑定类
     * @return Map<String,Object>
     */
    public Map<String,Object> handleValidMsg(BindingResult result) {
        log.info("处理验证信息！");
        Map<String,Object> map = new HashMap<>();
        if (result.hasErrors()) {// 验证不通过
            for(ObjectError objectError : result.getAllErrors()){
                map.put(((FieldError) objectError).getField(),objectError.getDefaultMessage());
            }
        }
        return map;
    }

}  